<template>
  <div v-loading="loading">
    <el-form ref="form"
      :model="form"
      :rules="rules"
      label-width="80px">

      <el-form-item label="金币上限" prop="species">
        <el-input-number v-model="form.species" :precision="0" :min="0"/>
      </el-form-item>

      <el-form-item label="兑换说明" prop="desc">
        <el-input type="textarea" :rows="6" v-model="form.desc"></el-input>
      </el-form-item>

      <el-form-item label="">
        <el-button type="primary" @click="submitForm">保存</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
import { exchangeLimit, updateExchangeLimit } from '@/api/activity/cfg'
export default {
  name: 'ExchangeCenter',
  data () {
    return {
      // 遮罩层
      loading: true,
      // 表单参数
      form: {
        species: 0,
        desc: ''
      },
      // 表单校验
      rules: {
        species: [
          { required: true, message: '金币上限不能为空', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '兑换说明不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getInfo()
  },
  methods: {
    /** 获取配置信息 */
    getInfo () {
      this.loading = true
      exchangeLimit().then(res => {
        const { id, species, desc } = res.data || {}
        this.form.id = id
        this.form.species = species
        this.form.desc = desc

        this.$nextTick(() => {
          this.$refs.form.clearValidate()
        })
      }
      ).finally(() => {
        this.loading = false
      })
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs['form'].validate(valid => {
        if (!valid) return
        const params = Object.assign({}, this.form)
        updateExchangeLimit(params).then(() => {
          this.msgSuccess('修改成功')
        })
      })
    }
  }
}
</script>
